﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PPIDesktopClient.Entities;
using VMind.Global;

namespace PPIDesktopClient.Controls.StructureTree
{
    public partial class StructureTree : UserControl
    {
        #region Private Properties
        private List<EVendor_Visit_XMissionStructure> m_VendorVisits;
        private List<EVendor_Visit_XMissionStructures_Pole> m_SelectedPoles;
        #endregion

        #region Private Methods
        private void AddNodes()
        {
            TreeNodeEx _Parent;
            TreeNodeEx[] _Childrens;
            try
            {
                foreach (EVendor_Visit_XMissionStructure vendor in m_VendorVisits)
                {
                    _Childrens = GetChildNodes(vendor);
                    _Parent = new TreeNodeEx(vendor.Id.ToString(), vendor.Id.ToString(), _Childrens);
                    treeView1.Nodes.Add(_Parent);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private TreeNodeEx[] GetChildNodes(EVendor_Visit_XMissionStructure vendor)
        {
            TreeNodeEx[] _Childrens;
            _Childrens = new TreeNodeEx[vendor.Poles.Count];

            for (int i = 0; i < vendor.Poles.Count; i++)
            {
                _Childrens[i] = new TreeNodeEx(vendor.Poles[i].Id.ToString());
                _Childrens[i].Name = vendor.Poles[i].Id.ToString();
            }
            return _Childrens;
        }
        #endregion

        #region Public Properties
        public List<EVendor_Visit_XMissionStructure> VendorVisits
        {
            get { return m_VendorVisits; }
            set { m_VendorVisits = value; }
        }
        public TreeNodeEx SelectedItem
        {
            get { return (TreeNodeEx)treeView1.SelectedNode; }
            set { treeView1.SelectedNode = value; }
        }
        #endregion

        #region Public Methods
        public override void Refresh()
        {
            //Suppress repainting the TreeView until all the objects have been created.
            treeView1.BeginUpdate();
            //Clear the TreeView each time the method is called.
            treeView1.Nodes.Clear();

            AddNodes();

            //Begin repainting the TreeView.
            treeView1.EndUpdate();
        }
        public void SelectAssets(List<EVendor_Visit_XMissionStructure> selectedAssets)
        {
            List<Int32> _SelectedIds = new List<Int32>();
            foreach (EVendor_Visit_XMissionStructure  _Asset in selectedAssets)
                _SelectedIds.Add(_Asset.Id);
            SelectAssets(_SelectedIds);
        }
        public void SelectAssets(List<Int32> selectedIds)
        {
            treeView1.BeginUpdate();
            foreach (TreeNodeEx _Node in treeView1.Nodes)
            {
                if (!_Node.Checked && _Node.Level == 0 && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Text)))
                {
                    _Node.Checked = true;
                    //m_QueryAssets.Add(new EUtility_QueryAsset(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                //if (_Node.Nodes.Count > 0)
                //    CheckQueryChildren(selectedIds, _Node);
            }
            treeView1.EndUpdate();
        }
        #endregion

        #region Constructor
        public StructureTree()
        {
            InitializeComponent();
            m_SelectedPoles = new List<EVendor_Visit_XMissionStructures_Pole>();
        }
        #endregion

        #region Events
        public event TreeViewEventHandler CheckedChange;
        private void OnCheckedChanged(object child, TreeViewEventArgs e)
        {
            // Safely invoke an event:
            if (CheckedChange != null)
            {
                CheckedChange(child, e);
            }
        }


        void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            TreeNode _ChangedStateNode = e.Node;
            int _SeletedTreeNodeID = int.Parse(_ChangedStateNode.Text);
            if (e.Node.Level == 0)
            {
                foreach (EVendor_Visit_XMissionStructure vendor in m_VendorVisits)
                {
                    if (_SeletedTreeNodeID == vendor.Id)
                    {
                        OnCheckedChanged(vendor, e);
                        return;
                    }
                }

            }
            else
            {
                foreach (EVendor_Visit_XMissionStructure vendor in m_VendorVisits)
                {
                    foreach (EVendor_Visit_XMissionStructures_Pole pole in vendor.Poles)
                    {
                        if (_SeletedTreeNodeID == pole.Id)
                        {
                            OnCheckedChanged(pole, e);
                            return;
                        }
                    }
                }            
            }


        }
        #endregion

    }
}
